package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private IItemService itemService;

    // 库存售出
    @Override
    public void updateStock(Long itemid, Integer num) {
        Item item = itemService.getById(itemid);
        if (num < item.getStock()) {
            int stock = item.getStock() - num;

            LambdaUpdateWrapper<Item> qw = new LambdaUpdateWrapper<>();
            qw.set(Item::getStock, stock);
            qw.in(Item::getId, itemid);
            itemService.update(qw);
        }else {
            throw new RuntimeException("库存不足！！！");
        }

    }

    // 库存恢复
    @Override
    public void updateStockRecover(Long itemId, Integer num1) {

        Item item = itemService.getById(itemId);
        int stock = item.getStock() + num1;

        LambdaUpdateWrapper<Item> qw = new LambdaUpdateWrapper<>();
        qw.set(Item::getStock, stock);

        qw.in(Item::getId, itemId);
        itemService.update(qw);

    }
}
